package com.topsun.posclient.finance.view;

import java.util.Iterator;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Table;
import org.eclipse.ui.actions.ActionGroup;

import com.topsun.posclient.finance.entity.FinanceEntity;

public class FinanceActionGroup extends ActionGroup {


	private TableViewer tv;
	
	public FinanceActionGroup(TableViewer tv){
		this.tv=tv;
	}
	
	@Override
	public void fillContextMenu(IMenuManager menu) {
		MenuManager menuMan=(MenuManager)menu;
		menuMan.add(new OpenAction());
		menuMan.add(new RefAction());
		menuMan.add(new DelAction());
		Table table=tv.getTable();
		Menu men=menuMan.createContextMenu(table);
		table.setMenu(men);
		super.fillContextMenu(menu);
	}
	
	private class OpenAction extends Action{
		
		public OpenAction(){
			setText("打开");
		}
		
		public void run(){
			IStructuredSelection selection=(IStructuredSelection)tv.getSelection();
			FinanceEntity o =(FinanceEntity)selection.getFirstElement();
			if(o==null){
				MessageDialog.openInformation(null, null, "选择记录");
			}
			else{
				MessageDialog.openInformation(null, null, o.getBank());

			}
		}
		
	}
	
	private class RefAction extends Action{
		
		public RefAction(){
			setText("刷新");
		}
		
		public void run(){
			tv.refresh();
		}
		
	}
	
	private class DelAction extends Action{
		
		public DelAction(){
			setText("删除");
		}
		
		public void run(){
			IStructuredSelection selection=(IStructuredSelection)tv.getSelection();
			if(selection.isEmpty()){
				MessageDialog.openInformation(null, null, "请选择记录");

			}
			else{
				for (Iterator it = selection.iterator(); it.hasNext();) {
					FinanceEntity fin =(FinanceEntity)it.next();
					tv.remove(fin);
				}
				
			}
		}
		
	}
}
